package com.ashin.user.controller;

import com.ashin.context.TenantIgnoreTableContext;
import com.ashin.user.entity.User;
import com.ashin.user.mapper.UserMapper;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 也可以使用注解InterceptorIgnore 进行忽略租户
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/noTenant")
public class UserNoTenantController {

    private final UserMapper userMapper;

    /**
     * 查询列表
     *
     * @return
     */
    @GetMapping(value = "/user")
    public List<User> queryUserList() {
        TenantIgnoreTableContext.setUserDefinedIgnoreTables(Lists.newArrayList("user"));
        List<User> users = userMapper.selectList(null);
        return users;
    }

    /**
     * 查询列表
     *
     * @return
     */
    @GetMapping(value = "/all/user")
    public List<User> queryAllUserList() {
        TenantIgnoreTableContext.setUserDefinedIgnoreTables(Lists.newArrayList("user"));
        List<User> users = userMapper.queryUserAllList();
        return users;
    }

    /**
     * 添加
     */
    @PostMapping(value = "/user")
    public void addUserList() {
        TenantIgnoreTableContext.setUserDefinedIgnoreTables(Lists.newArrayList("user"));

        userMapper.insert(User.builder().age(12).name("zjh").email("zjhptxl@163.com").build());
    }

    /**
     * 逻辑删除
     *
     * @param id
     */
    @DeleteMapping(value = "/user/{id}")
    public void deleteUser(@PathVariable("id") Integer id) {
        TenantIgnoreTableContext.setUserDefinedIgnoreTables(Lists.newArrayList("user"));

        userMapper.deleteById(id);
    }

    /**
     * 添加
     */
    @PutMapping(value = "/user/{id}")
    public void putUserList(@PathVariable Long id) {
        TenantIgnoreTableContext.setUserDefinedIgnoreTables(Lists.newArrayList("user"));

        userMapper.updateById(User.builder()
                .id(id)
                .age(12).name("zjh")
                .email("zjhptxl@163.com")
                .build());
    }
}
